#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "MarkerItem.h"
#include "Class.h"
#include "Bitmap.h"
#include "WindowSystem.h"
#include "String.h"

#include "marker.xpm3"
SmartBitmap markerBitmap(marker);

static Ink *markerInk;

//---- ChangeMarkerCommand -----------------------------------------------------

ChangeMarkerCommand::ChangeMarkerCommand(const char *cmd, int cmdNo,
		MarkerItem *mi, const char *nn, const char *nt) : Command(cmdNo, cmd)
{
	oldName= oldType= 0;
	newName= strsave(nn);
	newType= strsave(nt);
	item= mi;
}

ChangeMarkerCommand::~ChangeMarkerCommand()
{
	SafeDelete(newName);
	SafeDelete(oldName);
	SafeDelete(newType);
	SafeDelete(oldType);
}

void ChangeMarkerCommand::DoIt()
{
	if (oldName == 0) {
		oldName= strsave(item->Name());
		oldType= strsave(item->Type());
	}
	item->SetName(newName);
}

void ChangeMarkerCommand::UndoIt()
{
	item->SetName(oldName);
}

//---- MarkerItem --------------------------------------------------------------

NewMetaImpl(MarkerItem,VisualMark, (TS(name)));

MarkerItem::MarkerItem(const char *n, const char *t)
{
	name= strsave(n);
	type= strsave(t);
}

MarkerItem::~MarkerItem()
{
	SafeDelete(name);
	SafeDelete(type);
}

void MarkerItem::Draw(const Point &p, const Rectangle&, const Rectangle &lr, bool b)
{
	if (markerInk == 0) {
		if (GrHasColor())
			markerInk= new_RGBColor(0, 0, 255);
		else
			markerInk= gInkBlack;
	}
	if (b) {
		Rectangle r(p.x-4, lr.origin.y, 7, 4);
		GrPaintBitMap(r, markerBitmap, markerInk);
		//p.x--;
		//GrPaintLine(markerInk, 0, eDefaultCap, p, Point(p.x, p.y-4));
	}
}

OStream& MarkerItem::PrintOn (OStream &s)
{
	VisualMark::PrintOn(s);
	s.PrintString(name);
	return s.PrintString(type);
}

IStream& MarkerItem::ReadFrom(IStream &s)
{
	VisualMark::ReadFrom(s);
	SafeDelete(name);
	SafeDelete(type);
	s.ReadString(&name);
	return s.ReadString(&type);
}

void MarkerItem::SetName(const char *nm)
{
	strreplace(&name, nm);
}

void MarkerItem::SetType(const char *t)
{
	strreplace(&type, t);
}

